﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace QLTV_GiaoDien
{
    public partial class frm_PhieuMuonSach : DevExpress.XtraEditors.XtraForm
    {
        public frm_PhieuMuonSach()
        {
            InitializeComponent();
        }
        private int _Flag;

        public int Flag
        {
            get { return _Flag; }
            set { _Flag = value; }
        }
        private int _MaPhieuMuon;

        public int MaPhieuMuon
        {
            get { return _MaPhieuMuon; }
            set { _MaPhieuMuon = value; }
        }
        private int _MaDocGia;

        public int MaDocGia
        {
            get { return _MaDocGia; }
            set { _MaDocGia = value; }
        }
        private string _KieuMuon;

        public string KieuMuon
        {
            get { return _KieuMuon; }
            set { _KieuMuon = value; }
        }
        private DateTime _NgayMuon;

        public DateTime NgayMuon
        {
            get { return _NgayMuon; }
            set { _NgayMuon = value; }
        }
        public void Huy_Text(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Text = string.Empty;
            }
            foreach (Control i in ctr.Controls)
                Huy_Text(i);
        }
        public void EnableTextFalse(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Enabled =false;
            }
            foreach (Control i in ctr.Controls)
                EnableTextFalse(i);
        }
        public delegate void GetInt(int kt);
        public GetInt MyGetData;
        public void GetData()
        {
            txt_MaPM.Text = MaPhieuMuon.ToString();
            txt_MaDG.Text = MaDocGia.ToString();
            cob_KieuMuon.Text = KieuMuon;
            dateTime_NgayMuon.Text =NgayMuon.ToString();
        }
        public void LoadForm()
        {
            if (Flag == 0)
            {
                this.Text = "Thêm mới phiếu mượn";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
            }
            if (Flag == 1)
            {
                this.Text = "Sửa phiếu mượn";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                GetData();
            }
            if (Flag == 2)
            {
                this.Text = "Xem phiếu mượn";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                GetData();
                EnableTextFalse(this);
            }
        }
        PhieuMuon_BLL phieumuon_bll = new PhieuMuon_BLL();
        private void frm_PhieuMuonSach_Load(object sender, EventArgs e)
        {
            LoadForm();
        }

        private void bbi_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            PhieuMuon_Public pm = new PhieuMuon_Public();
            if (Flag == 0)
            {
                //them
                if (txt_MaDG.Text != "" && cob_KieuMuon.Text != "")
                {
                    pm.MaDocGia = int.Parse(txt_MaDG.Text);
                    pm.KieuMuon = cob_KieuMuon.Text;
                    pm.NgayLap = Convert.ToDateTime(dateTime_NgayMuon.Text);
                    phieumuon_bll.insert_PhieuMuon(pm, pm, pm);
                    XtraMessageBox.Show("Thêm mới thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Huy_Text(this);
                    bbi_Them.Enabled = true;
                    bbi_Sua.Enabled = true;
                    bbi_Luu.Enabled = false;
                    bbi_Huy.Enabled = false;
                }
                else
                    XtraMessageBox.Show("Bạn chưa nhập đủ dữ liệu","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            if (Flag == 1)
            {
                //sua

                pm.MaPhieuMuon = int.Parse(txt_MaPM.Text);
                pm.MaDocGia = int.Parse(txt_MaDG.Text);
                pm.KieuMuon = cob_KieuMuon.Text;
                pm.NgayLap = Convert.ToDateTime(dateTime_NgayMuon.Text);
                phieumuon_bll.update_PhieuMuon(pm,pm, pm, pm);
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Huy_Text(this);
                bbi_Them.Enabled = true;
                bbi_Sua.Enabled = true;
                bbi_Luu.Enabled = false;
                bbi_Huy.Enabled = false;
            }
            if (Flag == 2)
            {
                //xem
                pm.MaPhieuMuon = int.Parse(txt_MaPM.Text);
                pm.MaDocGia = int.Parse(txt_MaDG.Text);
                pm.KieuMuon = cob_KieuMuon.Text;
                pm.NgayLap = Convert.ToDateTime(dateTime_NgayMuon.Text);
                phieumuon_bll.update_PhieuMuon(pm,pm, pm, pm);
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Huy_Text(this);
                bbi_Them.Enabled = true;
                bbi_Sua.Enabled = true;
                bbi_Luu.Enabled = false;
                bbi_Huy.Enabled = false;
            }
            MyGetData(1);

        }

        private void bbi_Huy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Huy_Text(this);
            Flag = 4;
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
        }

        private void bbi_Thoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void txt_MaPM_Leave(object sender, EventArgs e)
        {
            if (txt_MaPM.Text == "")
                txt_MaPM.Focus();

        }

        private void txt_MaDG_Leave(object sender, EventArgs e)
        {
            if (txt_MaDG.Text == "")
                txt_MaDG.Focus();
        }

        private void txt_MaPM_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;

        }

        private void txt_MaDG_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 0;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
            txt_MaPM.Enabled = false;
            txt_MaPM.Text = "Mã tự sinh";
            this.Text = "Thêm Mới Phiếu Mượn Tài Liệu";
        }

        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 1;
            this.Text = "Sửa Phiếu Mượn Tài Liệu";
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
        }
        TaiLieu_BLL tailieu_bll = new TaiLieu_BLL();
        private void txt_MaDG_TextChanged(object sender, EventArgs e)
        {
            string sql = "select * from tbl_DocGia where MaDocGia='"+txt_MaDG.Text.Trim()+"'";
            DataTable dt = new DataTable();
            cob_TenDG.DataSource = dt = tailieu_bll.taoBang(sql);
            cob_TenDG.DisplayMember = "HoVaTen";
            if (dt.Rows.Count == 0)
            {
                errorProvider1.SetError(txt_MaDG, "Mã độc giả ko tồn tại!");
                bbi_Luu.Enabled = false;
                txt_MaDG.Focus();
            }
            else
            {
                errorProvider1.Clear();
                bbi_Luu.Enabled = true;
            }
        }
    }
}